# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
触发海拍客项目的接口
# post_hpk
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import hashlib
import time

import requests

from interfacetest.baseset.pethome_api import API_pethome
from interfacetest.baseset.test_logger import Logger
from msydwork.config.config_pethome import w_url
from msydwork.md5_encrypt import md5_upper


logger = Logger("post_hpk").get_log()

headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json'
        }
partner_key = 'e7800e6c27684ae3aecdc63a4c8c000a'
partner_no = 'RDPN0002'
merchant_no = '3223222'           # lw002647
outerProcessNo = "23232323"        # lw_01

#web验签
def web_sign():
    data = {
        "firstPayAmt": 10000,
        "loanAmt": 60000,
        "timestamp": "123",
        "partnerNo": partner_no,
        "outerProcessNo": outerProcessNo,
        "merchantNo": merchant_no,
        "loanTerm": 6
    }
    interface_sign(data)

# 外部接口加密
def interface_sign(data):
    parm = []
    for key in data:
        parm.append(key)
    parm.sort()
    encrypt = ''
    for k in parm:
        encrypt = encrypt + k + '=' + str(data[k]).replace(' ','').replace('\'','"')  + '&'
    encrypt = encrypt + 'partnerKey='+ partner_key
    md = md5_upper(encrypt)
    print('http://test.pet.msxiaodai.com/#/EntrancePage?'+encrypt.split('&partnerKey')[0] + '&sign='+md)
    return md

def trade_info():
    fu_url = w_url + '/ichong/credit/v1/tradeinfo/'
    data = API_pethome['trade_info']
    data['timestamp'] = str(round(time.time()*1000))
    data['merchantNo'] = merchant_no
    data['outerProcessNo'] = outerProcessNo
    sign = interface_sign(data)
    headers.update({'sign':sign})
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("交易信息推送信息接口响应信息：%s", data)

def credit_query():
    fu_url = w_url + '/ichong/credit/v1/query'
    data = {
        "timestamp":str(round(time.time()*1000)),
        "partnerNo":partner_no,
        "outerProcessNo": outerProcessNo,
    }
    sign = interface_sign(data)
    headers.update({'sign':sign})
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("授信查询接口响应信息：%s", data)

# 放款成功后
def repay_plan():
    fu_url = w_url + '/ichong/loan/v1/repayplan'
    data = {
        "timestamp":str(round(time.time()*1000)),
        "partnerNo":partner_no,
        "loanNo": "PETHOME202009271554169719cq6",
    }
    sign = interface_sign(data)
    headers.update({'sign':sign})
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("还款计划接口响应信息：%s", data)

# 生成借据未放款
def cancel_apply():
    fu_url = w_url + '/ichong/cancle/v1/apply'
    data = {
        "timestamp":str(round(time.time()*1000)),
        "partnerNo":partner_no,
        "loanNo": "PETHOME20201120163923320Wu7T",
    }
    sign = interface_sign(data)
    headers.update({'sign':sign})
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("申请贷款撤销（未放款）接口响应信息：%s", data)

def refund_apply():
    fu_url = w_url + '/ichong/refund/v1/apply'
    data = {
        "timestamp":str(round(time.time()*1000)),
        "partnerNo":partner_no,
        "loanNo": "PETHOME202011231713047348572",
        "refundType": "2",                        # 2-全部退货
        "refundAmt": 60000.00,
    }
    sign = interface_sign(data)
    headers.update({'sign':sign})
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("申请退货（已放款）接口响应信息：%s", data)

def risk_commit():
    fu_url = w_url + '/ichong/temp/riskCommit'
    data = API_pethome['riskCommit']
    data['creditAmt'] = '200000.00'    # N千
    data['idNo'] = '36242219881103431X'
    data['mobile'] = '15917374871'
    data['processNo'] = 'PRO2020101014392518768894222'
    data['riskResult'] = '2'   # 2-审批通过，3-审批拒绝
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("授信审批接口响应信息：%s", data)


if __name__ == '__main__':
    #print(str(int(time.time())) + '000')
    trade_info()
    #apply_loan(loan_date)
    #refund_apply()
    #refund_query()
    #loan_detail()
    #account('balance1')             # balance-查询余额
    #risk_commit()
    #credit_query()
    #repay_plan()
    #cancel_apply()
    #refund_apply()
    #web_sign()